package com.example.store.controller;

import com.example.store.common.JsonResult;
import com.example.store.service.ICarService;
import com.example.store.vo.CartVO;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.nio.file.AccessDeniedException;
import java.util.List;

@RestController
@RequestMapping("carts")
public class CartController extends BaseController {
    @Autowired
    private ICarService carService;
    @RequestMapping("add_to_cart")
    public JsonResult<Void> addToCart(Integer pid, Integer num, HttpSession session) {

        //Session中获取uid和usernamme
        Integer uid = getUidFromSession(session);
        String username = getUsernameFromSession(session);
        carService. addToCart(pid,num,uid,username);

        return new JsonResult<>(OK);
    }

    @RequestMapping({"","/"})
    public JsonResult<List<CartVO>> getVOByUid(HttpSession session) {
        Integer uid = getUidFromSession(session);
        List<CartVO> data = carService.getVOByUid(uid);
        return new JsonResult<List<CartVO>>(OK,data);

    }

    @RequestMapping("add_num")
    public JsonResult<Integer> addNum(Integer cid, HttpSession session) throws AccessDeniedException {
        Integer uid = getUidFromSession(session);
        String username = getUsernameFromSession(session);
        Integer data = carService.addNum(cid, uid, username);
        return new JsonResult<Integer>(OK,data);
    }
}
